home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Plotting / aa_Intel_Only / Gnuplot / WMInspector / GnuplotInspector.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  6.0 KB  |  304 lines

  1. /*
  2.  *  Copyright (C) 1993  Robert Davis
  3.  *
  4.  *  This program is free software; you can redistribute it and/or
  5.  *  modify it under the terms of Version 2, or any later version, of 
  6.  *  the GNU General Public License as published by the Free Software 
  7.  *  Foundation.
  8.  */
  9.  
  10. static char RCSId[]="$Id: GnuplotInspector.m,v 1.5 1993/05/30 20:09:53 davis Exp $";
  11.  
  12.  
  13. #import <appkit/Application.h>
  14. #import <appkit/Text.h>
  15. #import <appkit/TextField.h>
  16. #import <ctype.h>            /* isspace()    */
  17. #import <libc.h>            /* MAXPATHLEN    */
  18. #import <objc/NXBundle.h>
  19. #import <objc/objc.h>
  20.  
  21. #import "GnuplotInspector.h"
  22.  
  23. #define TYPE_UNKNOWN    1        /* plot types    */
  24. #define TYPE_TWOD    2
  25. #define TYPE_CONTOURS    4
  26. #define TYPE_POLAR    8
  27. #define TYPE_PARAMETRIC    16
  28.  
  29.  
  30. @interface GnuplotInspector (Private)
  31. - _getCommandsFromBuffer:(const char *)buf end:(const char *const)end;
  32. @end
  33.  
  34.  
  35. static const char *skipSpace (const char *cur, const char *const end)
  36. {
  37.     while (cur && (cur != end) && *cur && isspace (*cur))
  38.     cur++;
  39.     return cur;
  40. }
  41.  
  42.  
  43. static const char *skipToSpace (const char *cur, const char *const end)
  44. {
  45.     while (cur && (cur != end) && *cur && !isspace (*cur))
  46.     cur++;
  47.     return cur;
  48. }
  49.  
  50.  
  51. static int almost (const char *cur, const char *target, const char *const end)
  52. {
  53.     if (cur && target) {
  54.  
  55.     int i;
  56.     const char *c;
  57.     BOOL past = NO;
  58.  
  59.     i = 0;
  60.     for (c = cur; (c != end) && *c && !isspace (*c); c++)
  61.         i++;
  62.  
  63.     for (; i ; i--) {
  64.         if (*cur == *target) {
  65.         cur++;
  66.         target++;
  67.         } else {
  68.         if (*target == '$') {
  69.             past = YES;
  70.             i++;
  71.             target++;
  72.         } else
  73.             return NO;
  74.         }
  75.     }
  76.  
  77.     return (past || (*target == '$') || (*target == '\0'));
  78.  
  79.     }
  80.  
  81.     return NO;
  82. }
  83.  
  84.  
  85. static const char *getQuotedString (char *quoted, const char *from,
  86.                     const char *const end)
  87. {
  88.     if (from && (from != end)) {
  89.  
  90.     while ((from != end) && isspace(*from) && (*from != '\n'))
  91.         from++;
  92.  
  93.     if ((from != end) && ((*from == '\'') || (*from == '"'))) {
  94.  
  95.         char *cur;
  96.         char c = *(from++);
  97.  
  98.         cur = quoted;
  99.         while ((from != end) && *from && (*from != c) && (*from != '\n'))
  100.         *(cur++) = *(from++);
  101.         *cur = '\0';
  102.     }
  103.  
  104.     }
  105.  
  106.     return from;
  107. }
  108.  
  109.  
  110. @implementation GnuplotInspector
  111.  
  112. static id gnuplotInspector = nil;
  113.  
  114. + new
  115. {
  116.     if (gnuplotInspector == nil) {
  117.         char        path[MAXPATHLEN+1];
  118.         NXBundle    *bundle = [NXBundle bundleForClass:self];
  119.       
  120.         self = gnuplotInspector = [super new];
  121.         if ([bundle getPath:path 
  122.                 forResource:"GnuplotInspector"
  123.                 ofType:"nib"]) {
  124.  
  125.             [NXApp loadNibFile:path owner:gnuplotInspector];
  126.         } else {
  127.             fprintf (stderr, "Couldn't load GnuplotInspector.nib\n");
  128.             gnuplotInspector = nil;
  129.         }
  130.     }
  131.  
  132.     return gnuplotInspector;
  133. }
  134.  
  135.  
  136.  
  137. - ok:sender
  138. {
  139.     [super ok:sender];
  140.     return self;
  141. }
  142.  
  143.  
  144. - revert:sender
  145. {
  146.     NXStream    *s;
  147.     char    *text;
  148.     char    path[MAXPATHLEN];
  149.     int        len, maxlen;
  150.  
  151.     /*  Open a memory stream and get a pointer to the buffer */
  152.  
  153.     [self selectionPathsInto:path separator:':'];
  154.     if (s = NXMapFile (path, NX_READONLY)) {
  155.  
  156.     NXGetMemoryBuffer(s, &text, &len, &maxlen);
  157.  
  158.     [self _getCommandsFromBuffer:text end:text+len];
  159.     NXCloseMemory (s,NX_FREEBUFFER);
  160.  
  161.     if (type & TYPE_UNKNOWN)
  162.         strcpy (typestring, "Unknown");
  163.     else {
  164.         if (type & TYPE_TWOD) {
  165.         strcpy (typestring, "Two-dimensional");
  166.         if (type & TYPE_POLAR)
  167.             strcat (typestring, " Polar");
  168.         } else {
  169.         strcpy (typestring, "Three-dimensional");
  170.         if (type & TYPE_CONTOURS)
  171.             strcat (typestring, " with Contours");
  172.         }
  173.  
  174.         if (type & TYPE_PARAMETRIC)
  175.         strcat (typestring, " Parametric");
  176.     }
  177.  
  178.     [typeField setStringValue:typestring];
  179.     [titleText setText:title];
  180.     [expressionsText setText:expressions];
  181.  
  182.     }
  183.  
  184.     [super revert:sender];
  185.     return self;
  186. }
  187.  
  188.  
  189. @end
  190.  
  191.  
  192.  
  193. @implementation GnuplotInspector(Private)
  194.  
  195. - _getCommandsFromBuffer:(const char *)buf end:(const char *const) end
  196. {
  197.     const char    *cur;
  198.     char    *tc = title;
  199.     char    *ec = expressions;
  200.  
  201.     type = TYPE_UNKNOWN;
  202.     *title = '\0';
  203.     *expressions = '\0';
  204.  
  205.     cur = skipSpace (buf, end);
  206.     while (cur && (cur != end) && *cur) {
  207.  
  208.     if (almost (cur, "se$t", end)) {        /* set...    */
  209.         cur = skipSpace(skipToSpace (cur, end), end);
  210.  
  211.         if (almost (cur, "tit$le", end)) {        /* ...title */
  212.         cur = skipToSpace (cur, end);
  213.         cur = getQuotedString (tc, cur, end);
  214.  
  215.         } else if (almost (cur, "noco$ntour", end))
  216.         type &= ~TYPE_CONTOURS;
  217.  
  218.         else if (almost (cur, "co$ntour", end))
  219.         type |= TYPE_CONTOURS;
  220.         
  221.         else if (almost (cur, "nopo$lar", end))
  222.         type &= ~TYPE_POLAR;
  223.  
  224.         else if (almost (cur, "pol$ar", end))
  225.         type |= TYPE_POLAR;
  226.  
  227.         else if (almost (cur, "nopar$ametric", end))
  228.         type &= ~TYPE_PARAMETRIC;
  229.  
  230.         else if (almost (cur, "par$ametric", end))
  231.         type |= TYPE_PARAMETRIC;
  232.  
  233.     } else if (almost(cur,"p$lot",end) || almost(cur,"sp$lot",end)) {
  234.         type &= ~TYPE_UNKNOWN;
  235.         if (*cur == 'p')
  236.         type |= TYPE_TWOD;
  237.         else
  238.         type &= ~TYPE_TWOD;
  239.  
  240.         cur = skipSpace(skipToSpace (cur, end), end);
  241.         while (cur != end) {
  242.         if ((*cur == '\'') || (*cur == '"')) {
  243.             char     c = *(ec++) = *(cur++);
  244.             while ((cur != end) && (*cur != c) && (*cur != '\n'))
  245.             *(ec++) = *(cur++);
  246.             if (*cur == c)
  247.             *(ec++) = *(cur++);
  248.  
  249.         } else if (*cur == '(') {
  250.             int        count = 1;
  251.             while (count) {
  252.             *(ec++) = *(cur++);
  253.             if (cur != end) {
  254.                 if (*cur == '(')
  255.                 count++;
  256.                 else if (*cur == ')')
  257.                 count--;
  258.             } else
  259.                 break;
  260.             }
  261.             if (cur != end)
  262.             *(ec++) = *(cur++);
  263.             else
  264.             break;
  265.  
  266.         } else if (*cur == '\n') {
  267.             if (*(cur-1) == '\\') {
  268.             ec--;
  269.             cur = skipSpace(cur, end);
  270.             } else
  271.             break;
  272.  
  273.         } else if (*cur == ',') {
  274.             *(ec++) = '\n';
  275.             cur = skipSpace(cur+1, end);
  276.  
  277.         } else if (*cur == '#')
  278.             break;
  279.  
  280.         else
  281.             *(ec++) = *(cur++);
  282.         }
  283.         *ec = '\0';
  284.  
  285.         break;    /* Ignore eveything after the first plot command */
  286.     }
  287.  
  288.     while ((cur != end) && (*cur != '\n'))        /* Go to next line */
  289.         cur++;
  290.     cur = skipSpace (cur, end);
  291.     }
  292.  
  293.     return self;
  294. }
  295.  
  296.  
  297. // Shuts up the compiler about unused RCSId
  298. - (const char *) rcsid
  299. {
  300.     return RCSId;
  301. }
  302.  
  303. @end
  304.